<template>
  <div class="my-template">
    <div>
      <div class="my-l-butt">
        <router-link :to="{name: addPushName}">
          <el-button type="primary" plain>添加</el-button>
        </router-link>
        <el-button type="danger" plain @click="delObjs()">删除</el-button>
      </div>

      <div class="my-r-float">
        <el-input placeholder="请输入标题" v-model="inputObjName" class="input-with-select">
          <el-button slot="append" icon="el-icon-search" @click="inputFunc()"></el-button>
        </el-input>
      </div>

      <div>
        <!--内容组件-->
        <el-table
          ref="multipleTable"
          :data="tableData"
          class="my-put"
          tooltip-effect="dark"
          @selection-change="handleSelectionChange">

          <el-table-column
            type="selection"
            width="55">
          </el-table-column>

          <el-table-column
            prop="id"
            label="ID"
            width="50">
          </el-table-column>
          <el-table-column
            prop="sql_title"
            label="标题"
            width="200">
          </el-table-column>

          <el-table-column
            label="数据连接"
            width="120">
            <template slot-scope="scope">
              <el-popover trigger="hover" placement="top">
                <p>ID: {{ scope.row.database_info.id }}</p>
                <p>库名: {{ scope.row.database_info.db_name }}</p>
                <p>IP地址: {{ scope.row.database_info.db_ip }}</p>
                <p>端口号: {{ scope.row.database_info.db_port }}</p>
                <div slot="reference" class="name-wrapper">
                  <el-tag size="medium">{{ scope.row.database_info.db_name }}</el-tag>
                </div>
              </el-popover>
            </template>
          </el-table-column>

          <el-table-column
            prop="sql_template"
            label="模板"
            width="200">
            <template slot-scope="scope">
              <div class="mytable" v-text="scope.row.sql_template"></div>
            </template>
          </el-table-column>

          <el-table-column
            prop="sql_push_fields"
            label="入参"
            width="200">
            <template slot-scope="scope">
              <div class="mytable" v-text="scope.row.sql_push_fields"></div>
            </template>
          </el-table-column>

          <el-table-column
            prop="sql_data"
            label="合成sql"
            width="200">
            <template slot-scope="scope">
              <div class="mytable" v-text="scope.row.sql_data"></div>
            </template>
          </el-table-column>

          <el-table-column
            prop="sql_pull_fields"
            label="出参模板"
            width="200">
            <template slot-scope="scope">
              <div class="mytable" v-text="scope.row.sql_pull_fields"></div>
            </template>
          </el-table-column>

          <el-table-column
            prop="sql_test_data"
            label="出参数据"
            width="200">
            <template slot-scope="scope">
              <div v-show="scope.row.sql_test_data">
                <el-button type="text" size="small" @click="downTemplate(scope.row.sql_test_data)">下载
                </el-button>
                <el-link :href="getUrl(scope.row.sql_test_data)" target="_blank">查看
                </el-link>
              </div>
            </template>
          </el-table-column>

          <el-table-column
            prop="create_time"
            label="创建日期"
            width="200">
          </el-table-column>

          <el-table-column
            prop="update_time"
            label="更新日期"
            width="200">
          </el-table-column>

          <!--<el-table-column prop="status" label="测试状态" width="130">-->
          <!--<template slot-scope="scope">-->
          <!--<el-select size="mini" v-model="scope.row.test_status" disabled placeholder="请选择测试状态">-->
          <!--<el-option-->
          <!--v-for="(item,index) in optionsTestStatus"-->
          <!--:key="index"-->
          <!--:label="item.value"-->
          <!--:value="item.id">-->
          <!--</el-option>-->
          <!--</el-select>-->
          <!--</template>-->
          <!--</el-table-column>-->

          <el-table-column prop="status" label="状态" width="100">
            <template slot-scope="scope">
              <el-select size="mini" v-model="scope.row.status" disabled placeholder="请选择类型">
                <el-option
                  v-for="(item,index) in optionsCommon"
                  :key="index"
                  :label="item.value"
                  :value="item.id">
                </el-option>
              </el-select>
            </template>
          </el-table-column>

          <el-table-column
            fixed="right"
            label="操作"
            width="150">
            <template slot-scope="scope">
              <router-link :to="{name: etidPushName, query: { sqldetail_id: scope.row.id }}">
                <el-button type="text" size="small">编辑</el-button>
              </router-link>
              <el-button :disabled="scope.row.status===1" type="text" size="small" @click="delObjs(scope.row.id)">删除</el-button>
              <el-button type="text" size="small" @click="testObj(scope.row.id)">测试</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!--分页组件-->
      <div class="block paging">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="pageSizes"
          prev-text="上一页"
          next-text="下一页"
          layout="total, sizes, prev, pager, next, jumper"
          :total="dataCount">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    name: 'Sql',
    data() {
      return {
        addPushName: 'sqlAdd',
        etidPushName: 'sqlEdit',
        // 需要搜索的项目名称
        inputObjName: '',  // 搜索功能实现的字符串
        // choices设置
        optionsCommon: [],  // 存储CommonChoices实际数据的列表
        optionsTestStatus: [],


        // 分页设置
        currentPage: 1, //当前页码
        pageSize: 10,    //    每页的条数
        pageSizes: [10, 20, 30, 40],  // 可选每页显示条数
        dataCount: 0,  // 数据总条数

        // 数据设置
        ListDataURL: this.$http.sqlURL,  // 表格列表数据获取
        objID: 'sqldetail_id',
        tableData: [],  // 阿贾克斯获取的初始数据
        multipleSelection: [],  // 不能删，我也不知道是干嘛用的，删了会报错
      }
    },
    created() {
      // 发送啊假克丝请求
      this.objList();// 阿贾克斯获取的初始数据
      this.optionsCommon = this.getChoices.common;
      this.optionsTestStatus = this.getChoices.test_status;
    },

    methods: {

      handleClick(row) {
        console.log(row);
      },
      // 修改每页显示条数
      handleSizeChange(val) {
        this.pageSize = val;  // 修改每页显示条数
        this.currentPage = 1;  // 修改页码
      },
      // 修改页码
      handleCurrentChange(val) {
        this.currentPage = val;  // 修改页码
      },
      // 下载文件
      downTemplate(url) {
        this.$myTool.downTemplate(this, url)
      },
      // 跳转查看url
      getUrl(url) {
        return this.$http.HOST + url
      },

      // 发送啊假克丝请求 得到表格的原始数据
      objList() {
        this.$myTool.objList(this)
      },
      // 删除函数，调用了删除接口
      delObjs(objId) {
        this.$myTool.delObjs(objId, this)
      },
      // 测试sql功能
      testObj(objId) {
        let queryData = {
          id: objId
        };
        this.$http.getApiFunc(this.$http.getApiStatus)(this.$http.testSqlURL, queryData)
          .then(res => {
            this.$alert(res.data.msg, '提示', {
              confirmButtonText: '确定'
            });
            this.objList()
          })
          .catch(err => {
            console.log(err);
          });
      },

      // 不能删
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      //  按项目名称搜索的函数
      inputFunc() {
        this.$myTool.inputFunc(this)
      },
    },

    watch: {
      'pageSize': function (value) {
        this.objList();
      },
      'currentPage': function (value) {
        this.objList();
      }
    },

    computed: {
      getChoices() {
        return this.$store.state.choicesInfo
      }
    },
  }
</script>

<style scoped>
  .my-template{
    background-color: #FFFFFF;
    padding: 20px;
  }
  .my-l-butt{
    margin: 0 0 10px;
    float: left;
  }
  .my-r-float{
    float: right;
  }
  .my-put{
    width:100%;
  }
  .my-succes{
    color: #13ce66
  }
  .my-failure{
    color: red;
  }
  .my-no-in{
    color: sandybrown;
  }
  .paging {
    padding-top: 20px;
  }

  .mytable {
    /*width: 120px;*/
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

</style>
